﻿using System.Linq;
using dotMailer.Sdk.Collections;
using dotMailer.Sdk.Contacts.DataFields;
using dotMailer.Sdk.Enums;
using dotMailer.Sdk.Interfaces;

namespace ImportGoolgeContacts
{
    //-------------------------------------------------------------
    /// <summary>
    /// Encapsulation of google contact information
    /// 
    /// Also implements the IDmContact interface, so we can
    /// pass these object over to the dotMailer service, and 
    /// avoid having to do translation
    /// </summary>
    internal class DotMailerGoogleContact : IDmContact
    {
        public string Name { get; set; }
        public string EmailAddress1 { get; set; }
        public string EmailAddress2 { get; set; }
        public string Phone { get; set; }
        public string Details { get; set; }

        private readonly DmDataFieldValueCollection _dataFieldValues;

        //----------------------------------------------------------------
        public DotMailerGoogleContact(DmDataFieldValueCollection defaultDataFields)
        {
            _dataFieldValues = defaultDataFields;
        }

        /// <summary>
        /// returns true if the contact is usable in dotMailer
        /// </summary>
        public bool Valid
        {
            get
            {
                return (!string.IsNullOrEmpty(Name) &&
                        (!string.IsNullOrEmpty(EmailAddress1) || !string.IsNullOrEmpty(EmailAddress2)));
            }
        }

        #region IDmContact Interface
        /// <summary>
        /// default audience
        /// </summary>
        public ContactAudience Audience
        {
            get { return (ContactAudience.Unknown); }
        }

        /// <summary>
        /// Populates the data fields
        /// </summary>
        private void PopulateDatafields()
        {
            foreach (DmDataFieldValue value in _dataFieldValues.Where(value => value.Definition.IsSystem))
            {
                // we can only populate the email and full name datafields 
                switch (value.Definition.Name)
                {
                    case "FULLNAME":
                        value.Value = Name;
                        break;
                        
                    case "EMAIL":
                        value.Value = Email;
                        break;
                }
            }
        }

        /// <summary>
        /// datafields
        /// </summary>
        public DmDataFieldValueCollection DataFields
        {
            get 
            {
                PopulateDatafields();
                return (_dataFieldValues);
            }
        }

        /// <summary>
        /// email address -> dotMailer uses the email as the id for the contact
        /// </summary>
        public string Email
        {
            get
            {
                if (!string.IsNullOrEmpty(EmailAddress1))
                {
                    return(EmailAddress1);
                }
                
                if (!string.IsNullOrEmpty(EmailAddress2))
                {
                    return (EmailAddress2);
                }

                return ("");
            }
        }

        /// <summary>
        /// default to html emails
        /// </summary>
        public EmailContentTypes EmailType
        {
            get { return (EmailContentTypes.Html); }
        }

        /// <summary>
        /// System Id, will always be 0
        /// </summary>
        public int Id
        {
            get { return (0); }
        }


        /// <summary>
        /// Notes
        /// </summary>
        public string Notes
        {
            get { return (Details); }
        }


        /// <summary>
        /// Default optin
        /// </summary>
        public EmailOptIn OptIn
        {
            get { return (EmailOptIn.Single); }
        }

        /// <summary>
        /// this contact is read only, and will not change
        /// </summary>
        public bool IsDirty
        {
            get { return (false); }
        }
       

        //------------------------------------------------------
        /// <summary>
        /// Used by the IDmObject in the serialisation process..
        /// Any object that inherits from IDmObject can use the 
        /// Sdk Serialisation functions
        /// </summary>
        public string ObjectType
        {
            get { return (GetType().FullName); }
            set
            {
               // do nothing
            }
        }

        //------------------------------------------------------
        /// <summary>
        /// Flag to indicate that the datafields have been loaded from 
        /// the webservice. As we are creating this "dmContact" we 
        /// don't have access to the web service from here, so we 
        /// set the flag to true
        /// </summary>
        public bool HasDataFields
        {
            get { return (true); }
        }

        //------------------------------------------------------
        /// <summary>
        /// DotMailer Account Id
        /// </summary>
        public int AccountId
        {
            get { return (1); }
        }

        //------------------------------------------------------
        /// <summary>
        /// DotMailer External Id
        /// </summary>
        public string ExternalId
        {
            get { return ("0"); }
        }

        #endregion
    }
}
